package com.ruyuan.commodity.controller.seller;

import com.ruyuan.commodity.model.goods.dos.DraftGoodsDO;
import com.ruyuan.commodity.model.goods.dto.GoodsDTO;
import com.ruyuan.commodity.model.goods.request.GoodsInfoRequest;
import com.ruyuan.commodity.model.goods.vo.DraftGoodsVO;
import com.ruyuan.commodity.model.goods.vo.GoodsParamsGroupVO;
import com.ruyuan.commodity.model.goods.vo.GoodsSkuVO;
import com.ruyuan.commodity.model.goods.vo.GoodsVO;
import com.ruyuan.commodity.open.api.annotation.Auth;
import com.ruyuan.commodity.open.api.model.common.WebPage;
import com.ruyuan.commodity.common.mamager.DraftGoodsManager;
import com.ruyuan.commodity.common.mamager.DraftGoodsParamsManager;
import com.ruyuan.commodity.common.mamager.DraftGoodsSkuManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 草稿商品控制器
 */
@RestController
@RequestMapping("/seller/goods/draft-goods")
@Api(value = "草稿商品相关API")
public class GoodsDraftController {

    private static final Logger LOG = LoggerFactory.getLogger(GoodsDraftController.class);

    /**
     * 商品草稿箱业务管理器
     */
    @Autowired
    private DraftGoodsManager draftGoodsManager;
    /**
     * 商品草稿箱属性业务管理器
     */
    @Autowired
    private DraftGoodsParamsManager draftGoodsParamsManager;
    /**
     * 商品草稿箱SKU业务管理器
     */
    @Autowired
    private DraftGoodsSkuManager draftGoodsSkuManager;

    @Auth
    @ApiOperation(value = "查询草稿商品列表", response = DraftGoodsDO.class)
    @GetMapping
    public WebPage list(@ApiIgnore @NotEmpty(message = "页码不能为空") Long pageNo,
                        @ApiIgnore @NotEmpty(message = "每页数量不能为空") Long pageSize,
                        String keyword, @ApiIgnore String shopCatPath) {
        return draftGoodsManager.list(pageNo, pageSize, keyword, shopCatPath);
    }

    @Auth
    @ApiOperation(value = "添加商品", response = DraftGoodsDO.class)
    @ApiImplicitParam(name = "goods", value = "商品信息", required = true, dataType = "GoodsDTO", paramType = "body")
    @PostMapping
    public DraftGoodsDO add(@ApiIgnore @RequestBody GoodsDTO goods) {
        return draftGoodsManager.add(goods);
    }

    @Auth
    @PutMapping(value = "/{draft_goods_id}")
    @ApiOperation(value = "修改草稿商品", response = DraftGoodsDO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "draft_goods_id", value = "主键", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "goods", value = "商品信息", required = true, dataType = "GoodsDTO", paramType = "body")
    })
    public DraftGoodsDO edit(@RequestBody GoodsDTO goods, @PathVariable("draft_goods_id") Long draftGoodsId) {
        return draftGoodsManager.edit(goods, draftGoodsId);
    }

    @Auth
    @DeleteMapping(value = "/{draftGoodsIds}")
    @ApiOperation(value = "删除草稿商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "draftGoodsIds", value = "要删除的草稿商品主键", required = true, dataType = "int", paramType = "path")
    })
    public Map<String, Boolean> delete(@PathVariable("draftGoodsIds") Long[] draftGoodsIds) {
        Map<String, Boolean> result = new HashMap<>();
        try {
            draftGoodsManager.delete(draftGoodsIds);
            result.put("result", true);
            return result;
        } catch (Exception e) {
            LOG.error("删除草稿商品时，发生异常", e);
            result.put("result", false);
            return result;
        }
    }

    @Auth
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询一个草稿商品,商家编辑草稿商品使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的草稿商品主键", required = true, dataType = "int", paramType = "path")
    })
    public DraftGoodsVO get(@PathVariable Long id) {
        return this.draftGoodsManager.getVO(id);
    }

    @Auth
    @GetMapping(value = "/{draftGoodsId}/params")
    @ApiOperation(value = "查询草稿商品关联的参数，包括没有添加的参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "draftGoodsId", value = "要删除的草稿商品主键", required = true, dataType = "int", paramType = "path")
    })
    public List<GoodsParamsGroupVO> queryDraftParam(@PathVariable("draftGoodsId") Long draftGoodsId) {
        DraftGoodsDO draftGoods = draftGoodsManager.getModel(draftGoodsId);

        return draftGoodsParamsManager.getParamByCatAndDraft(draftGoods.getCategoryId(), draftGoodsId);
    }

    @Auth
    @ApiOperation(value = "查询草稿箱商品sku信息", notes = "商家中心编辑草稿箱商品时获得sku信息")
    @ApiImplicitParam(name = "draftGoodsId", value = "草稿商品id", required = true, dataType = "int", paramType = "path")
    @GetMapping(value = "/{draftGoodsId}/skus")
    public List<GoodsSkuVO> queryByDraftGoodsid(@PathVariable("draftGoodsId") Long draftGoodsId) {
        return draftGoodsSkuManager.getSkuList(draftGoodsId);
    }

    @Auth
    @ApiOperation(value = "修改草稿箱商品sku信息")
    @ApiImplicitParam(name = "draftGoodsId", value = "草稿商品id", required = true, dataType = "int", paramType = "path")
    @PutMapping(value = "/{draftGoodsId}/skus")
    public List<GoodsSkuVO> updateByDraftGoodsId(@PathVariable("draftGoodsId") Long draftGoodsId, @RequestBody List<GoodsSkuVO> skuList)  {

        draftGoodsSkuManager.add(skuList,draftGoodsId);

        return skuList;
    }

    @Auth
    @ApiOperation(value = "草稿箱商品上架接口", notes = "草稿箱商品上架时使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "draftGoodsId", value = "草稿商品id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "goods", value = "商品信息", required = true, dataType = "GoodsDTO", paramType = "body")
    })
    @PutMapping(value = "/{draftGoodsId}/market")
    public GoodsVO addMarket(@Valid @RequestBody GoodsInfoRequest request, @PathVariable("draftGoodsId") Long draftGoodsId) {
        return draftGoodsManager.addMarket(request, draftGoodsId);
    }

}
